﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class DemoDataSet_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = "";
        if (!IsPostBack)
            LoadManufacturer();
    }

    private void LoadManufacturer()
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from Manufacturer", DataAccess.ConnectionString);
        DataSet ds = new DataSet();
        da.Fill(ds);
        grvM.DataSource = ds.Tables[0];
        grvM.DataBind();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            SqlCommand cmd = DataAccess.Connection.CreateCommand();
            cmd.CommandText = "insert into Manufacturer values(@Mname,@Address,@Email,@Phone,@Describe,@Logo,@Lang,@Active)";
            //upload anh
            string logo=FileUpload1.FileName;
            if (FileUpload1.HasFile)
            {
                FileUpload1.SaveAs(Server.MapPath("~/images/" + logo));
            }
            //add value to parameter
            cmd.Parameters.AddWithValue("@Mname", txtName.Text);
            cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
            cmd.Parameters.AddWithValue("@email", txtEmail.Text);
            cmd.Parameters.AddWithValue("@phone", txtPhone.Text);
            cmd.Parameters.AddWithValue("@describe", txtDescribe.Text);
            cmd.Parameters.AddWithValue("@logo", logo);
            cmd.Parameters.AddWithValue("@lang", cboLang.SelectedItem.Value);
            cmd.Parameters.AddWithValue("@active", (chkActive.Checked)?1:0);
            cmd.ExecuteNonQuery();
            //re load data
            LoadManufacturer();
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }
    }
    protected void grvM_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        lblId.Text = grvM.Rows[e.NewSelectedIndex].Cells[1].Text;
        SqlCommand cmd = DataAccess.Connection.CreateCommand();
        cmd.CommandText = "select * from manufacturer where Mid=@id";
        cmd.Parameters.AddWithValue("@id", lblId.Text);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            if (dr.Read())
            {
                txtName.Text = dr.GetString(1);
                txtAddress.Text = dr.GetString(2);
                txtEmail.Text = dr.GetString(3);
                txtPhone.Text = dr.GetString(4);
                txtDescribe.Text = dr.GetString(5);
                cboLang.SelectedValue = dr.GetString(7);
                chkActive.Checked = (dr.GetInt32(8) == 1);
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = DataAccess.Connection.CreateCommand();
        cmd.CommandText = "update manufacturer set mname=@mname,address=@address,email=@email,phone=@phone,describe=@describe,logo=@logo,lang=@lang,active=@active where mid=@mid";
        string logo=FileUpload1.FileName;
            if (FileUpload1.HasFile)
            {
                FileUpload1.SaveAs(Server.MapPath("~/images/" + logo));
            }
        cmd.Parameters.AddWithValue("@mid", lblId.Text);
        cmd.Parameters.AddWithValue("@mname", txtName.Text);
        cmd.Parameters.AddWithValue("@address", txtAddress.Text);
        cmd.Parameters.AddWithValue("@email", txtEmail.Text);
        cmd.Parameters.AddWithValue("@phone", txtPhone.Text);
        cmd.Parameters.AddWithValue("@describe", txtDescribe.Text);
        cmd.Parameters.AddWithValue("@logo",logo);
        cmd.Parameters.AddWithValue("@lang", cboLang.SelectedItem.Value);
        cmd.Parameters.AddWithValue("@active", (chkActive.Checked) ? 1 : 0);
        cmd.ExecuteNonQuery();
        LoadManufacturer();
    }
}
